{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Fitting a line through data: with Machine Learning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "boston = datasets.load_boston()\n",
    "\n",
    "from sklearn.linear_model import LinearRegression\n",
    "lr = LinearRegression()\n",
    "\n",
    "lr.fit(boston.data, boston.target)\n",
    "\n",
    "from sklearn.model_selection import cross_val_predict\n",
    "\n",
    "predictions_cv = cross_val_predict(lr, boston.data, boston.target, cv=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0xd432438>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEACAYAAACj0I2EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFz9JREFUeJzt3X2MHIddxvHv4wRC07S+i8B3iLQ52lISKuhRoCClUk8k\nLmlRk0ggSHlRrtC/eAniD7ApL0EIgZ1/KiQEEqJwBvoGpZVdKMRFyVSCklIaHzHFMeHFaXm5RbhJ\nUQhEpf7xx875zne7np2dm5v97T4faZWdudnbx+P1L7vPzs4qIjAzs/wOdB3AzMz2hge6mdmU8EA3\nM5sSHuhmZlPCA93MbEp4oJuZTYnKgS7plZLOSHqs/O/nJN0vaV7SaUnnJT0k6eB+BDYzs8FU5zh0\nSQeAfwG+GfgR4GJEPCjpCDAfEUfbiWlmZlXqVi53AP8YEZ8B7gZOlOtPAPfsZTAzM6un7kD/buDd\n5fWFiOgBRMQGcGgvg5mZWT0jD3RJXwTcBfxBuWpnV+NzCJiZdejaGtu+EfhkRPxnudyTtBARPUmL\nwH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u4N/onznt08BbgXngz8o/x2lg\nruucQ7LfBnyh/Pd6pnzM3wncmCT/15aZ18u589Pl+hT5d/xZXs/WUS618/uDRWZmU8JfEm1mNiU8\n0M3MpoQHupnZlPBANzObEh7oZmZTwgPdzGxKeKCbmU0JD3Qzsynx/2I63TDaQqGkAAAAAElFTkSu\nQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0xd344be0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "#within Ipython \n",
    "%matplotlib inline \n",
    " \n",
    "pd.Series(boston.target - predictions_cv).hist(bins=50)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
